package com.ruoyi.web.controller.module;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.Coupons;
import com.ruoyi.system.service.module.wx.IWxUserService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.module.ICouponsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 平台抵扣券Controller
 *
 * @author ruoyi
 * @date 2022-04-28
 */
@Validated
@Api(value = "平台抵扣券控制器", tags = {"平台抵扣券管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/coupons")
public class CouponsController extends BaseController {

    private final ICouponsService couponsService;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

    /**
     * 查询平台抵扣券列表
     */
    @ApiOperation("查询平台抵扣券列表")
    @PreAuthorize("@ss.hasPermi('module:coupons:list')")
    @GetMapping("/list")
    public TableDataInfo<Coupons> list(@Validated Coupons coupons) {
        QueryWrapper<Coupons> qw = new QueryWrapper<>();
        if(StrUtil.isNotEmpty(coupons.getCouponName())){
        	qw.like("coupon_name",coupons.getCouponName());
		}

        TableDataInfo<Coupons> td = PageUtils.buildDataInfo(couponsService.page(PageUtils.buildPage(),qw));
        List<Coupons> list = td.getRows();
        if(CollUtil.isNotEmpty(list)){
        	for (Coupons coupons1 : list){
        		if(StrUtil.isNotEmpty(coupons1.getImgUrl())){
					coupons1.setImgUrlPath(imgUrl + coupons1.getImgUrl());
				}
			}
		}
        return td;
    }

    /**
     * 获取平台抵扣券详细信息
     */
    @ApiOperation("获取平台抵扣券详细信息")
    @PreAuthorize("@ss.hasPermi('module:coupons:query')")
    @GetMapping("/{id}")
    public AjaxResult<Coupons> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        Coupons coupons = couponsService.getById(id);
        if(coupons!=null){
			if(StrUtil.isNotEmpty(coupons.getImgUrl())){
				coupons.setImgUrlPath(imgUrl + coupons.getImgUrl());
			}
		}
        return AjaxResult.success(coupons);
    }

    /**
     * 新增平台抵扣券
     */
    @ApiOperation("新增平台抵扣券")
    @PreAuthorize("@ss.hasPermi('module:coupons:add')")
    @Log(title = "平台抵扣券", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody Coupons coupons) {
        return toAjax(couponsService.save(coupons) ? 1 : 0);
    }

    /**
     * 修改平台抵扣券
     */
    @ApiOperation("修改平台抵扣券")
    @PreAuthorize("@ss.hasPermi('module:coupons:edit')")
    @Log(title = "平台抵扣券", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody Coupons coupons) {
        return toAjax(couponsService.updateById(coupons) ? 1 : 0);
    }

    /**
     * 删除平台抵扣券
     */
    @ApiOperation("删除平台抵扣券")
    @PreAuthorize("@ss.hasPermi('module:coupons:remove')")
    @Log(title = "平台抵扣券" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(couponsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
